package edu.api.collections_api;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Demo1 {

    public static void main(String[] args) throws SQLException {
        test1();
    }


    public static void test1() throws SQLException {
        String sql = "select * from bs_book";
        ResultSet resultSet = JDBCUtils.query(sql, null);
        List<Bs_book> books = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String isbn = resultSet.getString("isbn");
            String name = resultSet.getString("name");
            String author = resultSet.getString("author");
            double new_price = resultSet.getDouble("new_price");
            Bs_book bs_book = new Bs_book(id, isbn, name, author, new_price);
            books.add(bs_book);
        }
        //将所有图书信息进行排序  根据价钱
        Collections.sort(books, new Comparator<Bs_book>() {
            @Override
            public int compare(Bs_book o1, Bs_book o2) {
                if (o1.getNew_price() - o2.getNew_price() > 0)
                    return 1;
                else if (o1.getNew_price() - o2.getNew_price() == 0)
                    return 0;
                return -1;
            }
        });

        /*
        基于lambda表达式简写实现
        Collections.sort(books, (Bs_book o1, Bs_book o2) -> {
            if (o1.getNew_price() - o2.getNew_price() > 0)
                return 1;
            else if (o1.getNew_price() - o2.getNew_price() == 0)
                return 0;
            return -1;
        });*/

        for (Bs_book book : books) {
            System.out.println("book = " + book);
        }
    }

}
